import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
import { IsignaLightStatus } from '../main/lib/C300ProtocolHandler'
import { ISignaLightConfig } from '../renderer/src/interface'

// Custom APIs for renderer
const api = {}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electronApi', {
      getSchemeList: (index:number) => ipcRenderer.invoke("getSchemeList", index),
      onSerialState:(cb)=>ipcRenderer.on('pub-serial-status',cb),
      getRealTimeStatus:()=> ipcRenderer.invoke('getRealTimeStatus'),
      onRunStatus: (callback: (arg: IsignaLightStatus) => void) => ipcRenderer.on('update-status', (_event, value) => callback(value)),
      sendENQ:()=>ipcRenderer.send("enq"),
      downloadScheme:(schemeList:string,schemeNum:number) => ipcRenderer.invoke("downloadScheme",schemeList,schemeNum),
      applyScheme:()=>ipcRenderer.invoke("applyScheme")
    })
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}
